package zengjin.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.PageResponse;
import zengjin.mapper.CollectMapper;
import zengjin.mapper.EmployerMapper;
import zengjin.mapper.JobMapper;
import zengjin.model.dto.collect.AddCollectRequest;
import zengjin.model.dto.collect.DeleteCollectRequest;
import zengjin.model.dto.collect.GetCollectRequest;
import zengjin.model.dto.collect.PageCollectRequest;
import zengjin.model.en.Collect;
import zengjin.model.en.Employer;
import zengjin.model.en.Job;
import zengjin.model.vo.employer.EmployerVO;
import zengjin.model.vo.job.JobVO;
import zengjin.service.CollectService;
import zengjin.service.EmployerService;
import zengjin.service.JobService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 19324
 * @description 针对表【collect】的数据库操作Service实现
 * @createDate 2025-02-18 19:59:13
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
        implements CollectService {

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private JobMapper jobMapper;

    @Resource
    private EmployerMapper employerMapper;

    @Override
    public Boolean addCollect(AddCollectRequest addCollectRequest) {
        //1. 获取参数
        Long collectJobseekerId = addCollectRequest.getCollectJobseekerId();
        String collectType = addCollectRequest.getCollectType();
        Long collectJobEmployerId = addCollectRequest.getCollectJobEmployerId();

        //查询是否已经收藏
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collect_jobseeker_id", collectJobseekerId)
                .eq("collect_type", collectType)
                .eq("collect_job_employer_id", collectJobEmployerId);
        if (collectMapper.selectOne(queryWrapper) != null) {
            return true;
        }

        //2. 添加收藏
        Collect collect = new Collect();
        collect.setCollectJobseekerId(collectJobseekerId);
        collect.setCollectType(collectType);
        collect.setCollectJobEmployerId(collectJobEmployerId);
        return collectMapper.insert(collect) > 0;
    }

    @Override
    public Object pageCollect(PageCollectRequest pageCollectRequest) {
        //1. 获取参数
        String searchName = pageCollectRequest.getSearchName();
        Long collectJobseekerId = pageCollectRequest.getCollectJobseekerId();
        String collectType = pageCollectRequest.getCollectType();

        //2.查出数据集合
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collect_jobseeker_id", collectJobseekerId)
                .eq("collect_type", collectType)
                .orderByDesc("update_time");
        if (searchName != null && !"".equals(searchName)) {
            queryWrapper.like("collect_id", searchName);
        }
        List<Collect> collects = collectMapper.selectList(queryWrapper);

        //3.查询出对象
        List<Object> collectsListVOS = new ArrayList<>();
        if("job".equals(collectType)){
            collects.forEach(collect -> {
                if (collect.getCollectJobEmployerId() != null) {
                    //查询出职位
                    Job job = jobMapper.selectById(collect.getCollectJobEmployerId());
                    if (job != null&& "正常".equals(job.getJobAdminStatus())) {
                        collectsListVOS.add(BeanUtil.copyProperties(job, JobVO.class));
                    }

                }
            });
        }else {
            collects.forEach(collect -> {
                if (collect.getCollectJobEmployerId() != null) {
                    Employer employer = employerMapper.selectById(collect.getCollectJobEmployerId());
                    if (employer != null&&"正常".equals(employer.getEmployerStatus())) {
                        collectsListVOS.add(BeanUtil.copyProperties(employer, EmployerVO.class));
                    }
                }
            });
        }

        //4.构建分页条件
        Page<Collect> page = pageCollectRequest.toPageDefaultSortByUpdateTime();
        return PageResponse.of(page, collectsListVOS);


    }

    @Override
    public boolean deleteCollect(DeleteCollectRequest deleteCollectRequest) {
        //1. 删除收藏
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collect_jobseeker_id", deleteCollectRequest.getCollectJobseekerId())
                .eq("collect_type", deleteCollectRequest.getCollectType())
                .eq("collect_job_employer_id", deleteCollectRequest.getCollectJobEmployerId());
        return collectMapper.delete(queryWrapper) > 0;
    }

    @Override
    public boolean getCollectByJobEmployerId(GetCollectRequest getCollectRequest) {
        //1. 查询是否已经收藏
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collect_jobseeker_id", getCollectRequest.getCollectJobseekerId())
                .eq("collect_type", getCollectRequest.getCollectType())
                .eq("collect_job_employer_id", getCollectRequest.getCollectJobEmployerId());
        return collectMapper.selectOne(queryWrapper) != null;
    }
}




